<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
//
/**
 * Excel下载
 * 变量  作用   数据类型
 * $xls 文件名 string
 * $data 数据 array
 * $table 字段名 array
 * $name 表格显示时的名字 array
 */
use app\index\model\Readmin as Readmins;
use think\Session;
use app\index\model\Draw;
use app\index\model\Agency;
function down($xls,$data,$name,$table)
{
    //调用类库,路径是基于vendor文件夹的
    Vendor('PHPExcel.PHPExcel');
    Vendor('PHPExcel.PHPExcel.Worksheet.Drawing');
    Vendor('PHPExcel.PHPExcel.Writer.Excel2007');
    $objExcel = new \PHPExcel();
    //set document Property
    $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');

    $objActSheet = $objExcel->getActiveSheet();
    $key = ord("A");
    $letter =explode(',',"A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T");
    $arrHeader = $name;
    //填充表头信息
    $lenth =  count($arrHeader);
    $objActSheet->setCellValue($letter[0]."1","序号");
    for($i = 0;$i < $lenth;$i++) {
        $objActSheet->setCellValue($letter[$i+1]."1","$arrHeader[$i]");
    };
    //填充表格信息
    //定义序号
    $j=1;
    foreach($data as $k=>$v){
        $k +=2;
        // 表格内容
        $t=0;
        $objActSheet->setCellValue($letter[0].$k,$j++);
        for($i=0;$i<count($name);$i++){
            $objActSheet->setCellValue($letter[$t+1].$k,$v[$table[$t++]]);
        }
        // 表格高度
        $objActSheet->getRowDimension($k)->setRowHeight(20);
    }
    //表格名字
    $outfile = $xls.date('Y-m-d H时i分s秒',time()).".xls";
    //其他配置信息
    ob_end_clean();
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header('Content-Disposition:inline;filename="'.$outfile.'"');
    header("Content-Transfer-Encoding: binary");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Pragma: no-cache");
    $objWriter->save('php://output');
    zsgc("下载了".$outfile);
}
/*
 * 记录操作
 * $name 操作内容
 */
function zsgc($name){
    //记录操作
    $Receipts=new Readmins([
        'adminid'=>session('id'),
        'record'=>$name,
        'times'=>time()
    ]);
    $Receipts->save();
}
//逆地理编码
function nibm($location){
    //用经纬度获得地址
        //全球逆地理编码
        $url = "http://api.map.baidu.com/geocoder/v2/?location=$location&output=json&pois=1&ak=aYXdZGaErvo3Zc5IBtp2kVwkNL2k7iSo";
        $res = file_get_contents($url);
        $js = json_decode($res,true);
        $str = $js['result']['formatted_address'];
        $str2 = $js['result']['addressComponent']['city'];
        $str3 = $js['result']['addressComponent']['district'];
   		 return $str.'|'.$str2.'|'.$str3;
}
/**
 * [删除回调方法]
 * @param  [type] $id  [状态码 0 失败 1 成功]
 * @param  [type] $msg [提示信息]
 * @return [type]      [返回json格式]
 */
function show($id,$msg){
    $arr=[];
    $arr['status']=$id;
    $arr['msg']=$msg;
    $param=json_encode($arr);
    echo $param;
}
function indexLogin()
{
	$isadmin=Session::has('isadmin');
	$url=url('Login/loginindex');
	if(!$isadmin){
		header('location:'.$url);
		die();
	}
}
function manageLogin()
{
    $userid=Session::has('userid');
    $isadmin=Session::has('isadmin');
    $url=url('Login/loginindex');
    if(!$userid){
        header('location:'.$url);
        die();
    }
}

/**
 * 跳转样式适应手机端
 * @return bool
 */
function isMobile()
{
    if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
    {
        return true;
    }
    if (isset ($_SERVER['HTTP_VIA']))
    {
        return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
    }
    if (isset ($_SERVER['HTTP_USER_AGENT']))
    {
        $clientkeywords = array ('nokia',
            'sony',
            'ericsson',
            'mot',
            'samsung',
            'htc',
            'sgh',
            'lg',
            'sharp',
            'sie-',
            'philips',
            'panasonic',
            'alcatel',
            'lenovo',
            'iphone',
            'ipod',
            'blackberry',
            'meizu',
            'android',
            'netfront',
            'symbian',
            'ucweb',
            'windowsce',
            'palm',
            'operamini',
            'operamobi',
            'openwave',
            'nexusone',
            'cldc',
            'midp',
            'wap',
            'mobile'
        );
        if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT'])))
        {
            return true;
        }
    }
    if (isset ($_SERVER['HTTP_ACCEPT']))
    {
        if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html'))))
        {
            return true;
        }
    }
    return false;
}
//修改时时间正常显示
function TimeAudit($time){
    $times = explode(" ", $time);
    $stoptimes = $times[0];
    $stoptimess = $times[1];
    $res = $stoptimes.'T'.$stoptimess;
    return $res;
}
//计算有多少提现信息
function drawnumber(){
    $draw=new Draw();
    echo $draw->number();
}
//计算有多少申请团长的记录
function managenumber(){
    $manage=new \app\index\model\Manage();
    echo $manage->number();
}
//计算未处理信息
function hownumber($a,$b){
    $res = Agency::where($a,$b)->field('count(id) as sum')->find();
    return $res['sum'];
}
//计算未处理代理商信息总数
function having(){
    $res1 = Agency::where('state',0)->field('count(id) as sum')->find();
    $res2 = Agency::where('withdrawal',1)->field('count(id) as sum')->find();
    return $res1['sum']+$res2['sum'];
}
